package com.org.platform.utils.mutitread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author duheng
 * @Date 2021/4/19 15:20
 */
public class ReentrantLockTest {

	private int i = 0;

	private Lock lock = new ReentrantLock();

	private Condition condition = lock.newCondition();

	public void setLock() {
		lock.tryLock();
		try {
			System.out.println("setlock");
			condition.await();
			System.out.println("unlock");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	public void unLock() {
		lock.tryLock();
		try {
			System.out.println("realse lock");
			condition.signalAll();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			lock.unlock();
		}
	}


	public static void main(String[] args) {
		ReentrantLockTest reentrantLockTest = new ReentrantLockTest();
		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				reentrantLockTest.setLock();
			}
		});
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {
			/*	try {
					t1.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}*/
				reentrantLockTest.unLock();
			}
		});
		t1.start();
		t2.start();
	}

}
